var locationList
getAjaxReq('location/listByToken', {}, function (res) {
    if (res.code == 1) {
        locationList = res.data;
        initTree(locationList);
    }
})

function initTree(data) {
    $('#jstree_div_layer').jstree({
        'core': {'data': data,"themes":{stripes:true}},
        "plugins": ["wholerow"]
    });
    $('#jstree_div_layer').bind("select_node.jstree", function (e, obj) {
        var currentNode = obj.node;
        var parents = currentNode.parents
        var name = '';
        if (parents) {
            parents.forEach(function (e) {
                var node = $('#jstree_div_layer').jstree("get_node", e)
                var t = (isEmpty(node.text)) ? '' : node.text + ' - '
                name = t + name;
            })
        }
        locationId = currentNode.id
        $("#addressSearch").val(name + currentNode.text);
        $("#tree_div").hide(200);
    });
    $("#addressSearch").on("click", function () {
        $("#tree_div").show(200);
    });
    $(document).on("click", function (event) {
        var evt = event.srcElement ? event.srcElement : event.target;
        if (evt.id != "addressSearch" && evt.id != "tree_div") {
            $("#tree_div").hide(200);
        } else {
            $("#tree_div").show(200);
        }
    });
    $("#tree_div").on("click", function (event) {
        event.stopPropagation();
    });
}

$("#emptyBtn").on("click", function () {
    $("#tree_div").hide(200)
    $("#addressSearch").val("")
    $("#jstree_div_layer").jstree('destroy')
    locationId = null
    locationList.forEach(function (e) {
        e.state = {selected: false}
    })
    initTree(locationList)
})